<template>
  <div class="costume-list">
    <!-- 页面头部 -->
    <div class="page-header">
      <h1 class="page-title">服装租赁</h1>
      <p class="page-subtitle">精选优质服装，满足您的各种场合需求</p>
    </div>

    <!-- 搜索筛选区域 -->
    <div class="filter-section">
      <el-card shadow="never">
        <el-form :model="searchParams" inline>
          <el-form-item label="服装名称">
            <el-input
                v-model="searchParams.keyword"
                placeholder="输入服装名称"
                clearable
                @keyup.enter="handleSearch"
                @clear="handleSearch"
            >
              <template #append>
                <el-button :icon="Search" @click="handleSearch" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="服装分类">
            <el-select
                v-model="searchParams.category"
                placeholder="选择分类"
                clearable
                @change="handleSearch"
            >
              <el-option label="全部" value="" />
              <el-option label="古装" value="古装" />
              <el-option label="现代装" value="现代装" />
              <el-option label="民族服装" value="民族服装" />
              <el-option label="戏服" value="戏服" />
            </el-select>
          </el-form-item>

          <el-form-item label="品牌">
            <el-select
                v-model="searchParams.brand"
                placeholder="选择品牌"
                clearable
                @change="handleSearch"
            >
              <el-option label="全部" value="" />
              <el-option label="汉尚华莲" value="汉尚华莲" />
              <el-option label="重回汉唐" value="重回汉唐" />
              <el-option label="明华堂" value="明华堂" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-card>
    </div>

    <!-- 服装列表 -->
    <div class="costume-content">
      <!-- 加载状态 -->
      <div v-if="costumeStore.loading" class="loading-container">
        <el-skeleton :rows="6" animated />
      </div>

      <!-- 空状态 -->
      <div v-else-if="costumeStore.costumeList.length === 0" class="empty-container">
        <el-empty description="暂无服装数据" />
      </div>

      <!-- 服装网格 -->
      <div v-else class="costume-grid">
        <el-row :gutter="20">
          <el-col
              v-for="costume in costumeStore.costumeList"
              :key="costume.id"
              :xs="12"
              :sm="8"
              :md="6"
              :lg="6"
              :xl="4"
          >
            <el-card
                class="costume-card"
                shadow="hover"
                :body-style="{ padding: '0' }"
                @click="viewCostumeDetail(costume.id)"
            >
              <div class="image-container">
                <el-image
                    :src="costume.imageUrl"
                    :alt="costume.name"
                    fit="cover"
                    class="costume-image"
                    :preview-src-list="[costume.imageUrl]"
                >
                  <template #error>
                    <div class="image-error">
                      <el-icon><Picture /></el-icon>
                      <span>图片加载失败</span>
                    </div>
                  </template>
                  <template #placeholder>
                    <div class="image-loading">
                      <el-icon><Loading /></el-icon>
                    </div>
                  </template>
                </el-image>
                <div class="costume-status">
                  <el-tag
                      :type="costume.status === 1 ? 'success' : 'danger'"
                      size="small"
                  >
                    {{ costume.status === 1 ? '可租' : '已租出' }}
                  </el-tag>
                </div>
              </div>

              <div class="costume-info">
                <h3 class="costume-name" :title="costume.name">
                  {{ costume.name }}
                </h3>
                <p class="costume-meta">
                  <span class="category">{{ costume.category }}</span>
                  <span class="brand">{{ costume.brand }}</span>
                </p>
                <p class="costume-spec">
                  <span>尺寸: {{ costume.size }}</span>
                  <span>颜色: {{ costume.color }}</span>
                </p>
                <div class="costume-price">
                  <span class="price">¥{{ costume.rentalPrice }}</span>
                  <span class="price-unit">/天</span>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
            v-model:current-page="costumeStore.pagination.pageNum"
            v-model:page-size="costumeStore.pagination.pageSize"
            :page-sizes="[12, 24, 48, 96]"
            :total="costumeStore.pagination.total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useCostumeStore } from '@/pinia/costumeStore'
import { Search, Picture, Loading } from '@element-plus/icons-vue'

const router = useRouter()
const costumeStore = useCostumeStore()

const searchParams = reactive({
  keyword: '',
  category: '',
  brand: ''
})

// 监听路由变化，处理浏览器前进后退
watch(
    () => router.currentRoute.value.query,
    (newQuery) => {
      if (newQuery.page) {
        costumeStore.pagination.pageNum = parseInt(newQuery.page) || 1
        loadCostumeList()
      }
    }
)

// 生命周期
onMounted(() => {
  // 从URL参数获取页码
  const pageFromUrl = parseInt(router.currentRoute.value.query.page)
  if (pageFromUrl && pageFromUrl > 0) {
    costumeStore.pagination.pageNum = pageFromUrl
  }
  loadCostumeList()
})

// 方法
const loadCostumeList = async () => {
  try {
    await costumeStore.fetchCostumeList(
        costumeStore.pagination.pageNum,
        costumeStore.pagination.pageSize
    )

    // 更新URL参数
    const query = { ...router.currentRoute.value.query }
    if (costumeStore.pagination.pageNum > 1) {
      query.page = costumeStore.pagination.pageNum
    } else {
      delete query.page
    }

    router.replace({ query })
  } catch (error) {
    console.error('加载服装列表失败:', error)
  }
}

const handleSearch = () => {
  costumeStore.pagination.pageNum = 1
  loadCostumeList()
}

const handleSizeChange = (newSize) => {
  costumeStore.pagination.pageSize = newSize
  costumeStore.pagination.pageNum = 1
  loadCostumeList()
}

const handleCurrentChange = (newPage) => {
  costumeStore.pagination.pageNum = newPage
  loadCostumeList()
}

const viewCostumeDetail = (id) => {
  router.push(`/costumes/${id}`)
}
</script>

<style scoped>
.costume-list {
  min-height: calc(100vh - 120px);
  background: #f5f7fa;
  padding: 20px 0;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
  padding: 0 20px;
}

.page-title {
  font-size: 32px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 10px;
}

.page-subtitle {
  font-size: 16px;
  color: #606266;
}

.filter-section {
  margin-bottom: 20px;
  padding: 0 20px;
}

.filter-section .el-card {
  border: none;
}

.costume-content {
  padding: 0 20px;
}

.costume-grid {
  margin-bottom: 30px;
}

.costume-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: none;
  border-radius: 8px;
  overflow: hidden;
}

.costume-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.image-container {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.costume-image {
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
}

.costume-card:hover .costume-image {
  transform: scale(1.05);
}

.image-error,
.image-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #999;
  background: #f5f7fa;
}

.costume-status {
  position: absolute;
  top: 8px;
  right: 8px;
}

.costume-info {
  padding: 15px;
}

.costume-name {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;
  line-height: 1.4;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.costume-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 12px;
  color: #909399;
}

.costume-spec {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
  font-size: 12px;
  color: #606266;
}

.costume-price {
  text-align: right;
}

.price {
  font-size: 20px;
  font-weight: bold;
  color: #e6a23c;
}

.price-unit {
  font-size: 12px;
  color: #909399;
  margin-left: 2px;
}

.loading-container {
  padding: 40px 0;
}

.empty-container {
  padding: 60px 0;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

@media (max-width: 768px) {
  .costume-list {
    padding: 10px 0;
  }

  .page-title {
    font-size: 24px;
  }

  .filter-section {
    padding: 0 10px;
  }

  .costume-content {
    padding: 0 10px;
  }

  .image-container {
    height: 150px;
  }
}
</style>